class node
{
    public:
    int val;
    node* next;
    node(int x)
    {
        val=x;
        next=nullptr;
    }
};
class MyLinkedList {
    private:
    node* Lhead;
public:
    MyLinkedList() {
        Lhead=new node(-1);
    }
    
    int get(int index) {
        node* cur=Lhead->next;
        while(cur&&index--)
        {
            cur=cur->next;
        }
        if(cur)
        return cur->val;
        else 
        return -1;
    }
    
    void addAtHead(int val) {
        node* newnode=new node(val);//创建节点
        node* tmp=Lhead->next;
        newnode->next=tmp;
        Lhead->next=newnode;
    }
    
    void addAtTail(int val) {
        node* newnode=new node(val);
        node* ptr=Lhead;
        while(ptr->next)
        {
            ptr=ptr->next;
        }
        ptr->next=newnode;
    }
    
    void addAtIndex(int index, int val) {
        node* ptr=Lhead;
        while(ptr&&index--)
        {
            ptr=ptr->next;
        }
        if(!ptr)return ;
        node* newnode=new node(val);
        newnode->next=ptr->next;
        ptr->next=newnode;

    }
    
    void deleteAtIndex(int index) {
        node* ptr=Lhead;
        while(ptr->next&&index--)
        {
            ptr=ptr->next;
        }
       if(ptr->next) ptr->next=ptr->next->next;
    }
};
